//
//  assetsOrderSellViewController.m
//  DBAChain
//
//  Created by Mxionlly on 2018/9/7.
//  Copyright © 2018年 Evan. All rights reserved.
//

#import "assetsOrderSellViewController.h"
#import "CommonData.h"
#import "JiaoYiPageDetailViewController.h"
#import "taskOverviewService.h"
#import "OrderQiuGouTableViewCell.h"
#import <Masonry.h>
#import "JiaoYiPageDetailsView.h"
#import "taskJiaoYiService.h"

@interface assetsOrderSellViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView      *tableView;
@property (nonatomic,assign)NSInteger      page;
@property (nonatomic,strong)NSArray<dataOrderBuyAndSaleInfo *> *  orderList;
@end

@implementation assetsOrderSellViewController

///////出售中

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self hideHeadView];
    [self.view addSubview:self.tableView];
//    [self loadOrder:1];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        [self loadOrder:1];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadMoreData];
    }];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.hidesBottomBarWhenPushed = NO;
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = NO;
    [self setBlackStatus:YES];
}

-(void)loadData{
    self.page = 1;
    [self loadOrder:1];
}

-(void)loadMoreData{
    self.page ++;
    [self loadOrder:self.page];
}

-(void)loadOrder:(NSInteger) pageIndex{
    NSMutableDictionary *dict = [NSMutableDictionary new];
    [dict setField:[NSString stringWithFormat:@"%ld",self.page] value:@"pageindex"];
    [dict setField:@"10" value:@"pagesize"];
    [dict setField:@"0" value:@"ordertype"];//交易类型：1 求购 0 出售
    [dict setValue:@(0) forKey:@"orderStatus"];//订单状态：0：等待交易，1：交易中 2：交易完成
    [dict setValue:@(1) forKey:@"isAll"];//是否查询所有：0：所有交易订单，1：个人交易订单
    [taskOverviewService getOrderBuyAndSaleList:dict success:^(dataOrderBuyAndSale *response, BOOL success) {
        [DKProgressHUD dismiss];
        [self.tableView.mj_footer endRefreshing];
         [self.tableView.mj_header endRefreshing];
        if (success) {
            self.orderList = response.data;
            [self.tableView reloadData];
            return ;
        }else{
            [DKProgressHUD showErrorWithStatus:response.message];
        }
    } failture:^(NSError *error, NSString *strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}


#pragma mark - tableview

-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-64) style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor  = [UIColor clearColor];
        _tableView.scrollEnabled = YES;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
    }
    return _tableView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.orderList count];
}

-(CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.5;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 74;
}
//定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete;
}
//修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"取消订单";
}
//点击删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { //在这里实现删除操作 //删除数据，和删除动画
    dataOrderBuyAndSaleInfo *m =  [self.orderList objectAtIndex:indexPath.row];
    dataOrderBuyAndSaleInfo *mm = [dataOrderBuyAndSaleInfo mj_objectWithKeyValues:m];
    [DKProgressHUD showLoading];
    NSMutableDictionary *dict=  [NSMutableDictionary new];
//    [dict setValue:mm.orderNo forKey:@"orderNo"];
    [taskOverviewService getCancelOrder:dict success:^(BaseModel *response, BOOL success) {
        [DKProgressHUD dismiss];
        if (success) {
            [DKProgressHUD showSuccessWithStatus:@"取消订单成功"];
            [self loadOrder:0];
        }else{
            [DKProgressHUD showErrorWithStatus:response.message];
        }
        
    } failture:^(NSError *error, NSString *strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
    
}

//先要设Cell可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OrderQiuGouTableViewCell *cell = [OrderQiuGouTableViewCell cellWithTableView:tableView IndexPath:indexPath];
    dataOrderBuyAndSaleInfo *m =  [self.orderList objectAtIndex:indexPath.row];
    dataOrderBuyAndSaleInfo *mm = [dataOrderBuyAndSaleInfo mj_objectWithKeyValues:m];
    cell.labelCNY.text = [NSString stringWithFormat:@"%@ CNY",mm.PriceCountRMB];
    cell.labelName.text = [NSString stringWithFormat:@"订单编码：%ld",mm.ID];
    cell.labelTime.text = [NSString stringWithFormat:@"%@     ",mm.CreateTimeName];
    cell.labelPrice.text = [NSString stringWithFormat:@"单价：%@CNY",mm.PriceRMB];
    cell.lbnum.text = [NSString stringWithFormat:@"数量：%@ china-A",mm.ChinaANumber];
    cell.lblimit.text = mm.TransactionStatusName;//[NSString stringWithFormat:@"限额：%@-%@ ",mm.LimitMix,mm.LimitMax];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [[UIAlertController alloc] alertTitle:@"提示" alertMessage:@"确定要取消订单吗？" leftCancelTitle:@"取消" leftCancel:nil rightTitle:@"确定" rightOK:^(UIAlertAction *action) {
        dataOrderBuyAndSaleInfo *m =  [self.orderList objectAtIndex:indexPath.row];
        dataOrderBuyAndSaleInfo *mm = [dataOrderBuyAndSaleInfo mj_objectWithKeyValues:m];
        
        [self cancelOrder:mm.ID];
    }];
}

-(void)cancelOrder:(NSInteger)orderid{
    
    NSMutableDictionary *dict = [NSMutableDictionary new];
    [dict setField:@(orderid) value:@"orderid"];
    [taskJiaoYiService cancelOrder:dict success:^(BaseModel *response, BOOL success) {
        [DKProgressHUD dismiss];
        if (success) {
            [DKProgressHUD showSuccessWithStatus:@"取消订单成功"];
            [self loadData];
            return ;
        }else{
            [DKProgressHUD showErrorWithStatus:response.message];
        }
    } failture:^(NSError *error, NSString *strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}

@end
